angular.module('dishApp', ['ngFileUpload'])
    .controller('FileUploadController', function ($scope, Upload, $rootScope, merchantService,newOrders) {
        //merchantService.merchantInfo();
        newOrders.newOrders();

        merchantService.merchantInfo(function(){
            if(!$rootScope.permission){
                window.location = 'login.html'
            }
            $scope.submit = function () {
                if ($scope.form.file.$valid && $scope.file && $scope.dname && $scope.price) {
                    $scope.upload($scope.file, $scope.dname, $scope.price);
                }
            };
        })


        $scope.upload = function (file, dname, price) {
            Upload.upload({
                url: '/dish/addDish',
                method: "POST",
                data:{dname:dname, price:price, merchant:$rootScope.merchant},
                file: file
            }).then(function (resp) {
                // console.log('Success ' + resp.config.file.name + 'uploaded. Response: ' + resp.file);
                file.result = resp.data;
                window.location = '#/dishList'
            }, function (resp) {
                console.log('Error status: ' + resp.status);
            }, function (evt) {
                file.progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                console.log('progress: ' + file.progressPercentage + '% ' + evt.config.file.name);
            });

        }
    })
    .controller("ListDishController", function ($scope, $rootScope, $http, $uibModal, merchantService, DishService) {
        $rootScope.active = 'dishList';
        merchantService.merchantInfo(function () {
            if(!$rootScope.permission){
                window.location = 'login.html'
            }
            //merchantService.merchantInfo();

            var req = {
                method: "GET",
                url: "/dish/loadActiveDish?mid="+$rootScope.merchant._id,
            };

            $http(req)
                .then(function (res) {
                    $scope.dishes = res.data;
                    console.log(res.data);
                    console.log($scope.dishes);
                });
        })

        $scope.isDelete = function (index) {
           var modalInstance = DishService.openWarningModal($uibModal, $scope, index);
        };

        $scope.deleteDish = function (index) {
            var req = {
                method: "DELETE",
                url: "/dish/deleteDish/" + $scope.dishes[index]._id
            }
            $http(req)
                .then(function (res) {
                    var result = res.data.msg;
                    if (result == 'SUCCESS') {
                        $scope.dishes.splice(index, 1);
                    }
                    else {
                        //
                    }
                });
        };

        $scope.isGroom = function (index) {
            var modalInstance = DishService.openGroomModal($uibModal, $scope, index);
        };
        $scope.recommendDish = function (index) {
            var req = {
                method: "GET",
                url: "/dish/recommendDish/" + $scope.dishes[index]._id
            }
            $http(req)
                .then(function (res) {
                    var result = res.data.msg;
                    if (result == 'SUCCESS') {
                        $scope.dishes[index].status = 2;
                    }
                    else {
                        //..
                    }
                });
        }
    })
    .controller("DeleteModelController", function ($scope, $http, $uibModalInstance, parentScope, index) {
        $scope.confirm = function () {
            parentScope.deleteDish(index);
            $scope.cancel();
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    })
    .controller("GroomModelController", function ($scope, $http, $uibModalInstance, parentScope, index) {
        $scope.confirm = function () {
            parentScope.recommendDish(index);
            $scope.cancel();
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });